package ar.com.mindset.geometrics_wi.colorScheme;

import ar.com.mindset.geometrics_wi.exceptions.GeometricsRuntimeException;

import java.util.HashMap;
import java.util.Map;


/**
 * @author http://www.mindset-designs.com
 */
public class DiscreteColorScheme extends WithLegendColorScheme {

    private String defaultColor;
    private String name;
    private Map<String, String> discreteValues;


    public DiscreteColorScheme() {
        discreteValues = new HashMap<String, String>();
    }

    public void setDefaultColor(String defaultColor) {
        this.defaultColor = defaultColor;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addDiscreteValue(String key, String color) {
        this.discreteValues.put(key, color);
    }

    @Override
    protected void writeColorScheme(StringBuffer xml) {
        xml.append("<discrete");
        if (this.defaultColor != null) {
            xml.append(" defaultColor='").append(this.defaultColor).append("'");
        }
        if (this.name != null) {
            xml.append(" name='").append(this.name).append("'");
        }
        xml.append(">");

        if (this.legend != null) {
            this.legend.write(xml);
        }
        this.writeDiscreteValues(xml);

        xml.append("</discrete>");
    }

    private void writeDiscreteValues(StringBuffer xml) {
        if (discreteValues.size() == 0) {
            throw new GeometricsRuntimeException("Must set discrete values for the Discrete Color Scheme");
        }

        xml.append("<discreteValues>");
        for (String key : this.discreteValues.keySet()) {
            xml.append("<colorEntry key='").append(key).append("' color='").append(this.discreteValues.get(key)).append("'/>");
        }
        xml.append("</discreteValues>");
    }
}
